package com.example.controller;

import com.example.task.MyAsyncTask;
import com.example.task.AsyncTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/demo")
public class DemoController {

    private final Logger logger = LoggerFactory.getLogger(DemoController.class);

    @Autowired
    private AsyncTask task;

    @Autowired
    private MyAsyncTask myAsyncTask;

    @GetMapping("async_task")
    public String exeTask() throws Exception {

        long begin = System.currentTimeMillis();

        // 异步
		task.task1();
		task.task2();
		task.task3();

        myAsyncTask.task1();
        myAsyncTask.task2();
        myAsyncTask.task3();


//		// 异步结果
//        Future<String> task4 = task.task4();
//        Future<String> task5 = task.task5();
//        Future<String> task6 = task.task6();
//        for (; ; ) {
//            if (task4.isDone() && task5.isDone() && task6.isDone()) {
//                logger.info("所有任务执行完成！");
//                break;
//            }
//        }

        long end = System.currentTimeMillis();
        long total = end - begin;
        logger.info("执行总耗时=" + total);

        return "异步响应完成";
    }
}
